package com.chapp.provider.handler;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.util.Log;

import com.chapp.base.Location;
import com.chapp.base.Venue;
import com.chapp.provider.DroidContentProvider;

public class VenueHandler {

	public static final Uri CONTENT_URI = Uri.parse("content://"
			+ DroidContentProvider.AUTHORITY + "/venue");
	public static final String ID = "id_venue";
	public static final String NAME = "name";
	public static final String LOCATION_ID = "location";
	public static final String URL = "url";
	public static final String IMAGE = "image";
	public static final String PHONE = "phone";
	public static final String PARENT = "parent";

	public static Uri insertVenue(Context context, Venue venue, int parent) {
		Uri location = LocationHandler.insertLocation(context,
				venue.getLocation());
		String locationID = location.getLastPathSegment();

		ContentValues valores = new ContentValues();
		valores.put(ID, venue.getId());
		valores.put(IMAGE, venue.getThumbnailURL());
		valores.put(NAME, venue.getName());
		valores.put(PHONE, venue.getPhonenumber());
		valores.put(URL, venue.getUrl());
		valores.put(LOCATION_ID, locationID);
		valores.put(PARENT, parent);

		Uri uri = context.getContentResolver().insert(CONTENT_URI, valores);
		Log.i(uri.getPath(), uri.getPath());

		return uri;
	}

	/*
	 * Consulta por id y padre
	 */
	public static Venue getVenue(Context context, String id, int parent) {
		Uri uri = Uri.withAppendedPath(CONTENT_URI, "" + id);
		Cursor cursor = context.getContentResolver().query(uri, null,
				"parent = " + parent, null, null);
		Venue venue = new Venue();
		if (cursor.moveToNext()) {
			venue.setId(cursor.getString(cursor.getColumnIndex(VenueHandler.ID)));
			venue.setName(cursor.getString(cursor
					.getColumnIndex(VenueHandler.NAME)));
			venue.setPhonenumber(cursor.getString(cursor
					.getColumnIndex(VenueHandler.PHONE)));
			venue.setThumbnailURL(cursor.getString(cursor
					.getColumnIndex(VenueHandler.IMAGE)));
			venue.setUrl(cursor.getString(cursor
					.getColumnIndex(VenueHandler.URL)));
			venue.setLocation(setLocation(cursor.getString(cursor
					.getColumnIndex(VenueHandler.LOCATION_ID)), context));
			cursor.close();
			return venue;
		}
		return null;
	}

	/**
	 * Consulta los que han sido marcados favoritos
	 * 
	 * @param context
	 * @return
	 */

	public static List<Venue> getAllVenues(Context context) {
		List<Venue> venues_list = new ArrayList<Venue>();
		Cursor cursor = context.getContentResolver().query(CONTENT_URI, null,
				null, null, null);
		if (cursor.moveToFirst()) {
			do {
				Venue venue = new Venue();
				venue.setId(cursor.getString(cursor
						.getColumnIndex(VenueHandler.ID)));
				venue.setName(cursor.getString(cursor
						.getColumnIndex(VenueHandler.NAME)));
				venue.setPhonenumber(cursor.getString(cursor
						.getColumnIndex(VenueHandler.PHONE)));
				venue.setThumbnailURL(cursor.getString(cursor
						.getColumnIndex(VenueHandler.IMAGE)));
				venue.setUrl(cursor.getString(cursor
						.getColumnIndex(VenueHandler.URL)));
				venue.setLocation(setLocation(cursor.getString(cursor
						.getColumnIndex(VenueHandler.LOCATION_ID)), context));
				venues_list.add(venue);
			} while (cursor.moveToNext());
		}
		cursor.close();
		return venues_list;
	}

	private static Location setLocation(String string, Context context) {
		return LocationHandler.getLocation(context, Integer.valueOf(string));
	}

	public static boolean deleteVenue(Context context, int id) {
		int rows = context.getContentResolver().delete(CONTENT_URI,
				"id_venue = " + id, null);
		if (rows != 0) {
			return true;
		}
		return false;
	}
}
